package com.novellotus.iov.carvice.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

/**
 * Created by Rich on 2017/5/10.
 * 自定义分页布局
 */

public class PageView extends RelativeLayout {

    //显示数据的recycleView
    public RecyclerView recyclerView;
    //加载中布局
    public LoadingView loadingView;

    public RefreshLayout refreshLayout;
    //请求数据的总页数
    public int totalPage = 1;
    //请求数据当前页数
    public int currentPage = 1;
    //是否加载完
    public boolean loadFinish = true;

    private PageViewInterface pageViewInterface;

    public PageView(Context context) {
        super(context);
    }

    public PageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }


    /**
     * 在刷新布局里添加
     * RecycleView和LoadingView
     */
    private void initView(Context context, AttributeSet attrs) {

        LayoutInflater.from(context).inflate(R.layout.layout_loading_recycle, this, true);
        loadingView = findViewById(R.id.loadingView);
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);

        refreshLayout.setDirection(RefreshLayoutDirection.BOTH);
        //滑动到底部自动加载数据
        recyclerView.addOnScrollListener(new OnRcvScrollListener() {
            @Override
            public void onBottom() {
                super.onBottom();
                // 到底部自动加载

                if (loadFinish) {
                    //下拉加载更多(当前页数小于总页数时)
                    if (currentPage < totalPage) {
                        if (adapterBottomInterface != null)
                            adapterBottomInterface.hideBottomLayout();
                        setLoadFinish(false);
                        currentPage++;
                        pageViewInterface.loadData(currentPage);
                        refreshLayout.setRefreshing(true);
                    } else {
                        if (adapterBottomInterface != null)
                            adapterBottomInterface.showBottomLayout();
                        refreshLayout.setRefreshing(false);
                        refreshLayout.setDirection(RefreshLayoutDirection.TOP);
                    }
                }
            }
        });
    }


    /**
     * 根据返回数据的总数和页面数量计算出页数
     */
    public void setTotalPage(int count, int size) {
        try {
            totalPage = count / size;
            if (totalPage == 0) {
                totalPage = 1;
            } else {
                //余数不为0页数加1
                if (count % size != 0) {
                    totalPage++;
                }
            }
        } catch (Exception e) {
            MyUtils.logInformation(e.toString());
            totalPage = 1;
        }
        if (totalPage == 1) {
            refreshLayout.setDirection(RefreshLayoutDirection.TOP);
        }
    }

    public void setReLoadBtnListener(LoadingView.OnBtnClickListener loadBtnListener) {
        loadingView.setOnBtnClickListener(loadBtnListener);
    }

    /**
     * 设置接口实现类
     */
    public void setPageViewInterface(PageViewInterface pageViewInterface) {
        this.pageViewInterface = pageViewInterface;
    }


    /**
     * 设置加载状态
     */
    public void setLoadFinish(boolean loadFinish) {
        this.loadFinish = loadFinish;
        refreshLayout.setRefreshing(false);
    }

    /**
     * 设置刷新方向
     */
    public void setRefreshDirection() {
        if (currentPage < totalPage) {
            refreshLayout.setDirection(RefreshLayoutDirection.BOTH);
        } else {
            refreshLayout.setDirection(RefreshLayoutDirection.TOP);
        }
    }

    /**
     * 设置当前请求数据页数
     */
    public void setLoadCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * 返回当前数据页数
     */
    public int getLoadCurrentPage() {
        return currentPage;
    }

    /**
     * 默认是加载状态
     */
    public void initLoadingView(LoadingView.OnBtnClickListener listener) {
        loadingView.setLoadingModel(LoadingView.MODEL_DEFAULT);
        loadingView.setOnBtnClickListener(listener);
        loadingView.loading();
    }

    /**
     * 默认是加载状态
     */
    public void initLoadingDialog(LoadingView.OnBtnClickListener listener) {
        loadingView.setLoadingModel(LoadingView.MODEL_ALERT);
        loadingView.setOnBtnClickListener(listener);
        loadingView.loading();
    }

    /**
     * 载成功
     */
    public void loadSuccess() {
        loadingView.success();
        setLoadFinish(true);
        recyclerView.setVisibility(View.VISIBLE);
    }

    /**
     * @param msg 加载失败
     */
    public void loadFail(String msg, boolean button) {
        loadingView.empty(msg, button);
        setLoadFinish(true);
        recyclerView.setVisibility(View.GONE);
    }


    /**
     * 设置RecycleView数据
     */
    public <T extends RecyclerView.ViewHolder> void setRecyclerViewAdapter(RecyclerView.LayoutManager layoutManager, RecyclerView.Adapter<T> adapter) {
        loadingView.success();
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    AdapterBottomInterface adapterBottomInterface;

    public void setBottomInterface(AdapterBottomInterface adapterBottomInterface) {
        this.adapterBottomInterface = adapterBottomInterface;
    }

    /**
     * 如果是向下拉,刷新数据
     */
    public void refreshData(RefreshLayoutDirection direction) {
        if (direction == RefreshLayoutDirection.TOP) {
            MyUtils.logInformation("刷新");
            setLoadCurrentPage(1);
            pageViewInterface.loadData(currentPage);
            refreshLayout.setDirection(RefreshLayoutDirection.BOTH);
        } else {
            MyUtils.logInformation("加载");
            if (currentPage <= totalPage) {
                currentPage++;
                if (currentPage == totalPage) {
                    refreshLayout.setDirection(RefreshLayoutDirection.TOP);
                }
                pageViewInterface.loadData(currentPage);
            }
        }
    }

    /**
     * 通知实现类调用相关方法
     */
    public interface PageViewInterface {
        void loadData(int dataPage);
    }


}
